// 封装axios实例的拦截器(请求,响应)
import axios from "axios";
import Cookies from "js-cookie";
const instance = axios.create({
  timeout: 15000,
  // baseURL: "http://jingqiao.free.idcfengye.com",
  baseURL: "http://120.24.64.5:8088/mall-admin",
});
// 重写实例请求前拦截器
instance.interceptors.request.use(
  (config) => {
    let token = Cookies.get("token");
    // console.log(token);
    if (token) {
      // 统一设置请求头
      config.headers = config.headers || {};
      config.headers.Authorization = token;
    }
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

// 重写实例响应后拦截器
instance.interceptors.response.use(
  (res) => {
    return res.data; //这里后台返回的数据需要.data是我们所需要的数据
  },
  (err) => {
    return Promise.reject(err);
  }
);

export default instance;
